package com.yyq.gxdc.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yyq.gxdc.bean.Result;
import com.yyq.gxdc.entity.Coupon;
import com.yyq.gxdc.entity.ReceiveCoupon;
import com.yyq.gxdc.entity.User;
import com.yyq.gxdc.service.ICouponService;
import com.yyq.gxdc.service.IReceiveCouponService;
import com.yyq.gxdc.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@Controller
@RequestMapping("/coupon")
public class CouponController {

    @Autowired
    private ICouponService couponService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IReceiveCouponService receiveCouponService;

    /**
     * 分页查找
     * @param name
     * @param pageNum
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/front/couponList")
    @ResponseBody
    public Result<IPage<Coupon>> couponList(
            String name,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "8") Integer pageSize,
            HttpServletRequest req) {

        Result<IPage<Coupon>> result = new Result<IPage<Coupon>>();
        Page<Coupon> page = new Page<Coupon>(pageNum, pageSize);
        Coupon coupon = new Coupon();
        coupon.setName(name);
        IPage<Coupon> allCoupon = couponService.getAllCoupon(page, coupon);

        ReceiveCoupon receiveCoupon = new ReceiveCoupon();
        IPage<ReceiveCoupon> allReceiveCoupon =null;

        Object curruser = req.getSession().getAttribute("user");
        Page<ReceiveCoupon> pageReceiveCoupon = new Page<ReceiveCoupon>(pageNum, pageSize);
        // 根据当前登录人 判断 是管理员 还是 非管理员
        if(null == curruser){
            allReceiveCoupon = new Page<ReceiveCoupon>();
        }else{
            User user = userService.getById((Integer)curruser);
            if("1".equals(user.getUsetype())){
                // 如果userTYpe 是1  则是管理员 看到所有的
                allReceiveCoupon = receiveCouponService.getAllReceiveCoupon(pageReceiveCoupon, receiveCoupon);
            }else{
                // 负责 只看到自己的
                receiveCoupon.setUid((int)curruser);
                allReceiveCoupon = receiveCouponService.getAllReceiveCoupon(pageReceiveCoupon, receiveCoupon);
            }
        }

        List<Coupon> recordsCoupon= allCoupon.getRecords();
        List<ReceiveCoupon> records = allReceiveCoupon.getRecords();
        List<Coupon> newRecordsCoupon= allCoupon.getRecords();
        if(records.size()>0  && recordsCoupon.size()>0){
            for (Coupon coupon1:recordsCoupon) {
                for (ReceiveCoupon receiveCoupon1:records) {
                    if(coupon1.getId().equals(receiveCoupon1.getCid())){
                        coupon1.setCFlag("1");
                    }
                }
            }
        }
        allCoupon.setRecords(recordsCoupon);
        result.setSuccess(true);
        result.setResult(allCoupon);
        return result;
    }

    /**
     * 分页查找自己的优惠卷
     * @param name
     * @param pageNum
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/front/myCouponList")
    @ResponseBody
    public Result<IPage<Coupon>> myCouponList(
            String name,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "8") Integer pageSize,
            HttpServletRequest req) {

        Result<IPage<Coupon>> result = new Result<IPage<Coupon>>();
        Page<Coupon> page = new Page<Coupon>(pageNum, pageSize);
        Coupon coupon = new Coupon();
        coupon.setName(name);
        IPage<Coupon> allCoupon = couponService.getAllCoupon(page, coupon);

        ReceiveCoupon receiveCoupon = new ReceiveCoupon();
        IPage<ReceiveCoupon> allReceiveCoupon =null;

        Object curruser = req.getSession().getAttribute("user");
        Page<ReceiveCoupon> pageReceiveCoupon = new Page<ReceiveCoupon>(pageNum, pageSize);
        // 根据当前登录人 判断 是管理员 还是 非管理员
        if(null == curruser){
            allReceiveCoupon = new Page<ReceiveCoupon>();
        }else{
            receiveCoupon.setUid((int)curruser);
            allReceiveCoupon = receiveCouponService.getAllReceiveCoupon(pageReceiveCoupon, receiveCoupon);
        }

        List<Coupon> recordsCoupon= allCoupon.getRecords();
        List<ReceiveCoupon> records = allReceiveCoupon.getRecords();
        List<Coupon> newRecordsCoupon= new ArrayList<>();
        if(records.size()>0  && recordsCoupon.size()>0){
            for (Coupon coupon1:recordsCoupon) {
                for (ReceiveCoupon receiveCoupon1:records) {
                    if(coupon1.getId().equals(receiveCoupon1.getCid())){
                        newRecordsCoupon.add(coupon1);
                    }
                }
            }
        }
        allCoupon.setRecords(newRecordsCoupon);
        result.setSuccess(true);
        result.setResult(allCoupon);
        return result;
    }


    /**
     * 后台分页查找
     * @param name
     * @param pageNum
     * @param pageSize
     * @param model
     * @return
     */
    @GetMapping("/mgr/couponList")
    @ResponseBody
    public Result<IPage<Coupon>> mgrCouponList(
            String name,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10000") Integer pageSize,
            Model model) {

        Result<IPage<Coupon>> result = new Result<IPage<Coupon>>();
        Page<Coupon> page = new Page<Coupon>(pageNum, pageSize);
        Coupon coupon = new Coupon();
        coupon.setName(name);
        IPage<Coupon> allCoupon = couponService.getAllCoupon(page, coupon);
        result.setSuccess(true);
        result.setResult(allCoupon);
        return result;
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @GetMapping("/getCouponDetail")
    @ResponseBody
    public Result<Coupon> getCouponDetail(int id, HttpServletRequest req) {
        Result<Coupon> result = new Result<>();
        Coupon detailByRid = couponService.getDetailByRid(id);
        result.setResult(detailByRid);
        result.setSuccess(true);
        return result;
    }

//    /**
//     * 根据rid 获取景点详情图片
//     * @param rid
//     * @return
//     */
//    @GetMapping("/getRouteImgsDetail")
//    @ResponseBody
//    public Result<List<RouteImg>> getRouteImgsDetail(int rid, HttpServletRequest req) {
//        Result<List<RouteImg>> result = new Result<>();
//        List<RouteImg> routeImgByRid = routeImgService.getRouteImgByRid(rid);
//        result.setResult(routeImgByRid);
//        result.setSuccess(true);
//        return result;
//    }

    /**
     * 添加优惠卷管理信息
     * @param coupon
     * @param req
     * @return
     */
    @PostMapping(value = "/addCoupon")
    @ResponseBody
    public Result<Object> addUser(Coupon coupon, HttpServletRequest req) {
        // 新增 菜品信息
        couponService.add(coupon);
        return Result.ok();
    }

    /**
     * 删除菜品
     * @param id
     * @param req
     * @return
     */
    @PostMapping(value = "/deleteCoupon")
    @ResponseBody
    public Result<Object> deleteCoupon(int id, HttpServletRequest req) {
        // 先查询 这个 菜品 是否已存在
        Coupon byId = couponService.findById(id);
        if(byId == null ){
            return Result.error("该优惠卷不存在！");
        }else{
            couponService.delete(id);
            return Result.ok();
        }
    }

    /**
     * 修改优惠卷信息
     * @param coupon
     * @param req
     * @return
     */
    @PostMapping(value = "/updateCoupon")
    @ResponseBody
    public Result<Object> updateCoupon(Coupon coupon, HttpServletRequest req) {
        // 先查询 这个 优惠卷 是否已存在
        Coupon byId = couponService.findById(coupon.getId());
        if(byId == null ){
            return Result.error("该优惠卷不存在！");
        }else{
            couponService.update(coupon);
        }
            return Result.ok();
    }

    /**
     * 编辑详情
     * @param id
     * @return
     */
    @GetMapping("/getDetailByRid")
    @ResponseBody
    public Result<Coupon> getDetailByRid(int id, HttpServletRequest req) {
        Result<Coupon> result = new Result<>();
        // 先查询 这个 优惠卷是否已存在
        Coupon byId = couponService.findById(id);
        if(byId == null ){
            result.setSuccess(false);
            result.setMessage("该优惠卷不存在！");
            return result;
        }else{
            result.setResult(byId);
            result.setSuccess(true);
            return result;
        }
    }
}
